package com.personal.system.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.personal.common.result.Result;
import com.personal.model.system.SysDept;
import com.personal.model.system.SysLoginLog;
import com.personal.system.domain.test.TcGasSaleQuotation;
import com.personal.system.domain.test.TcGasSaleQuoteDetail;
import com.personal.system.entity.CompanyInfo;
import com.personal.system.mapper.sys.LoginLogMapper;
import com.personal.system.mapper.test.TcGasSaleQuoteDetailMapper;
import com.personal.system.service.act.CompanyInfoService;
import com.personal.system.service.sys.SysDeptService;
import com.personal.system.service.test.TcGasSaleQuotationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@Slf4j
@RestController
@RequestMapping("/company")
public class CompanyInfoController {

    @Resource
    private CompanyInfoService companyInfoService;
    @Resource
    private SysDeptService deptService;
    @Resource
    private LoginLogMapper loginLogMapper;
    @Resource
    private TcGasSaleQuotationService quotationService;
    @Resource
    private TcGasSaleQuoteDetailMapper quoteDetailMapper;

    @GetMapping("/list")
    public Result list(CompanyInfo companyInfo) {
        List<CompanyInfo> list = companyInfoService.list();
        return Result.ok(list);
    }

    // 异步任务测试
    @GetMapping("/completableFuture")
    public Result completableFuture() {
        Map<Object, Object> map = new HashMap<>();
        long start = System.currentTimeMillis();
        CompletableFuture<List<CompanyInfo>> future1 = CompletableFuture.supplyAsync(() -> {
            List<CompanyInfo> infos = companyInfoService.list();
            return infos;
        });

        CompletableFuture<List<SysDept>> future2 = CompletableFuture.supplyAsync(() -> {
            List<SysDept> deptList = deptService.list();
            return deptList;
        });
        CompletableFuture<List<SysLoginLog>> future3 = CompletableFuture.supplyAsync(() -> {
            List<SysLoginLog> sysLoginLogs = loginLogMapper.selectList(Wrappers.emptyWrapper());
            return sysLoginLogs;
        });
        CompletableFuture<List<TcGasSaleQuotation>> future4 = CompletableFuture.supplyAsync(() -> {
            List<TcGasSaleQuotation> quotations = quotationService.list();
            return quotations;
        });
        CompletableFuture<List<TcGasSaleQuoteDetail>> future5 = CompletableFuture.supplyAsync(() -> {
            List<TcGasSaleQuoteDetail> details = quoteDetailMapper.selectList(Wrappers.emptyWrapper());
            return details;
        });

        // 使用 thenApply() 方法在异步任务完成后执行一个函数
        CompletableFuture<String> stringCompletableFuture = future1.thenApply(s -> s + " World");
        // 使用 thenAccept() 方法在异步任务完成后执行一个操作
        CompletableFuture<Void> result1 = future1.thenAccept(s -> {
            System.out.println(s + " World");
        });
        // 使用 thenCompose() 方法连接两个异步任务
        CompletableFuture<String> strThen = future1.thenCompose(s -> {
            // 在第一个异步任务完成后执行的逻辑，返回另一个 CompletableFuture 对象
            return CompletableFuture.supplyAsync(() -> s + " World");
        });

        // 使用 allOf() 方法等待多个异步任务完成后执行一个操作  anyOf()方法等待多个异步任务中的任意一个完成后执行一个操作
        CompletableFuture<Void> resultV = CompletableFuture.allOf(future1, future2, future3, future4,future5)
                .thenRun(() -> {
            // 在所有异步任务完成后执行的操作
//            System.out.println(future1.join() + " " + future2.join() + " " + future3.join()
//                                + " " + future4.join()+ " " + future5.join());
            map.put("infos" , future1.join().size());
            map.put("deptList" , future2.join().size());
            map.put("sysLoginLogs" , future3.join().size());
            map.put("quotations" , future4.join().size());
            map.put("details" , future5.join().size());
        });
        // 等待所有异步计算完成
        resultV.join();

        long end = System.currentTimeMillis();
        System.out.println("用时：" + (end - start));
        return Result.ok(map);
    }

}
